import type {AppMo} from '@/service/entity/AppMo';
import type {DataBox} from '@/service/entity/DataBox';
import type {MenuMo} from '@/service/entity/MenuMo';
import type {UserMo} from '@/service/entity/UserMo';
import fetchData from '@/utils/fetchData';
import type {GetListArgs} from "@/service/entity/GetListArgs";
import type {AppRoleMo} from "@/service/entity/AppRoleMo";
import type {AppPermStrMo} from "@/service/entity/AppPermStrMo";
import type {PermStrDao} from "@/service/entity/PermStrDao";
import axios from "axios";
import {srvHost} from "@/config/Host";
import {ElMessage} from "element-plus";
import {getAdminToken} from "@/utils/setToken";

const proxy = async (path: string, data?: any) => {
    return await fetchData('/srv-auth/admin/app/' + path, data)
}

export const adminGetAppList = async (args: GetListArgs): Promise<{
    arr: AppMo[]
    total: number
}> => {
    return await proxy('adminGetAppList', args)
}

export const adminGetAppUserList = async (args: GetListArgs): Promise<{
    arr: UserMo[]
    total: number
}> => {
    return await proxy('adminGetAppUserList', args)
}

export const authMenuToRole = async (roleId: any, menuIds: any[]): Promise<DataBox> => {
    return await proxy('authMenuToRole', {roleId, menuIds})
}

export const exportAppSettingsInsertSql = async (appId: any, appName?: string) => {
    const res = await axios.get(srvHost + '/srv-auth/upDownload/exportAppSettingsInsertSql/'+appId, {
        responseType: 'blob',
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'token': getAdminToken()
        }
    }).catch(err => {
        try {
            ElMessage.error(err.response.data.message)
        } catch (_err) {
            ElMessage.error('未知错误')
        }
    })
    if (!res) {
        return
    }
    const {data, headers} = res
    const fileName = `${appName || appId}配置插入语句.sql`
    const blob = new Blob([data], {type: headers['content-type']})
    const dom = document.createElement('a')
    const downUrl = window.URL.createObjectURL(blob)
    dom.href = downUrl
    dom.download = decodeURIComponent(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom.parentNode.removeChild(dom)
}

export const delApp = async (appId: any): Promise<DataBox> => {
    return await proxy('delApp', {appId})
}

export const delAppPermStr = async (appId: any, permStrId: any[]): Promise<DataBox> => {
    return await proxy('delAppPermStr', {appId, permStrId})
}

export const delAppRole = async (roleId: any): Promise<DataBox> => {
    return await proxy('delAppRole', {roleId})
}

export const delAppUser = async (appId: any, uid: any): Promise<DataBox> => {
    return await proxy('delAppUser', {appId, uid})
}

export const delMenu = async (menuId: any): Promise<DataBox> => {
    return await proxy('delMenu', {menuId})
}

export interface EditAppArgs {
    activeRule: string
    appId: any
    appType: string
    appName: string
    frontIp: string
    frontPort: number
    serverIp: string
    serverPort: number
}

export const editApp = async (args: EditAppArgs): Promise<AppMo> => {
    return await proxy('editApp', args)
}

export interface EditAppRoleArgs {
    appId: any
    roleId?: any
    roleLevel: number
    roleName: string
    ids: any[]
}

export const editAppRole = async (args: EditAppRoleArgs): Promise<AppRoleMo> => {
    return await proxy('editAppRole', args)
}

export interface EditAppUserArgs {
    appId: any
    roleIds: any[]
    uid: any
}

export const editAppUser = async (args: EditAppUserArgs): Promise<any> => {
    return await proxy('editAppUser', args)
}

export interface UpdatePermStrArgs {
    id: any
    isMenu: boolean
    symbolStr: string
}

export const updatePermStr = async (args: UpdatePermStrArgs): Promise<DataBox> => {
    return await proxy('updatePermStr', args)

}

export interface EditMenuArgs {
    appId: any
    icon: string
    symbolStr: string
    component: string
    menuId: any
    parentId: any
    pathUrl: string
    title: string
    idx: number
    params: number
    status: number
}

export const editMenu = async (args: EditMenuArgs): Promise<MenuMo> => {
    return await proxy('editMenu', args)
}

export interface EditAppPermStrArgs {
    appId: any
    menuId: any
    symbolStr: any[]

}

export const editAppPermStr = async (args: EditAppPermStrArgs): Promise<AppPermStrMo> => {
    return await proxy('editAppPermStr', args)
}

export const getAppMenuList = async (appId: any): Promise<{
    arr: MenuMo[]
}> => {
    return await proxy('getAppMenuList', {appId})
}

export const getRoleSelectedMenuNodeIds = async (roleId: any): Promise<{
    arr: any[]
}> => {
    return await proxy('getRoleSelectedMenuNodeIds', {roleId})
}

export const getRoleSelectedMenuLeaf = async (roleId: any): Promise<{
    arr: any[]
}> => {
    return await proxy('getRoleSelectedMenuLeaf', {roleId})
}

export const getUserAllAppList = async (): Promise<{
    arr: AppMo[]
    total: number
}> => {
    return await proxy('getUserAllAppList')
}

export const switchApp = async (appId: any): Promise<{
    appName: string
    roleLevel: number
    rootPath: string
    token: string
    isAdmin: boolean
    permStrs: string[]
    menuList: MenuMo[]
    roles: AppRoleMo[]
}> => {
    return await proxy('switchApp', {appId})
}

export const getAppRoleList = async (args: GetListArgs): Promise<{
    arr: AppRoleMo[]
    total: number
}> => {
    return await proxy('getAppRoleList', args)
}

export const getRoleSelectedPermStr = async (appId: any, roleId: any): Promise<{
    arr: any[]
}> => {
    return await proxy('getRoleSelectedPermStr', {appId, roleId})
}
export const getPermStrTreeData = async (appId: any): Promise<{
    arr: PermStrDao[]
}> => {
    return await proxy('getPermStrTreeData', {appId})

}

export const getUserSelectedRoleIds = async (appId: any, uid: any): Promise<{
    arr: any[]
}> => {
    return await proxy('getUserSelectedRoleIds', {appId, uid})
}

export const getMenuLeafPermStrList = async (menuId: any): Promise<{
    arr: string[]
}> => {
    return await proxy('getMenuLeafPermStrList', {menuId})
}

export const getAppOpenUrl = async (appId: any, uid: any, pagePath: string, authKey: string): Promise<DataBox> => {
    return await proxy('getAppOpenUrl', {appId, uid, pagePath, authKey})
}

export const openApp = async (openToken: any, authKey: any): Promise<{
    appName: string
    roleLevel: number
    rootPath: string
    token: string
    isAdmin: boolean
    uname: string
    name: string
    uid: any
    nginxBasePath: string
    frontIp: string
    frontPort: number
    permStrs: string[]
    menuList: MenuMo[]
    roles: AppRoleMo[]
}> => {
    return await proxy('openApp', {openToken, authKey})
}